package org.xbmc.android.util;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;
import org.xbmc.api.object.MusicAlbumVideoList;
import org.xbmc.api.object.MusicArtist;
import org.xbmc.api.object.MusicCategoryList;
import org.xbmc.api.object.MusicInfo;
import org.xbmc.constants.INetConstants;

/**
 * parse Json file
 * 
 * @author Thanhnv
 * 
 */
public class JsonParsingUtils implements INetConstants {

	public static ArrayList<MusicCategoryList> parseListMusicCategory(
			String response) {
		if (response != null && response.length() > 0) {
			try {
				JSONArray mJsonArray = new JSONArray(response);
				int sizeJson = mJsonArray.length();
				ArrayList<MusicCategoryList> mLiApplicationObjects = new ArrayList<MusicCategoryList>();
				for (int i = 0; i < sizeJson; i++) {
					JSONObject mJsonObject = mJsonArray.getJSONObject(i);
					String name = mJsonObject.getString(NAME);
					String type = mJsonObject.getString(TYPE);
					int cid = mJsonObject.getInt(CID);
					MusicCategoryList mApplicationObject = new MusicCategoryList(cid, name, type);
					mLiApplicationObjects.add(mApplicationObject);
				}

				return mLiApplicationObjects;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public static ArrayList<MusicAlbumVideoList> parseListMusicAlbum(
			String response) {
		if (response != null && response.length() > 0) {
			try {
				JSONArray mJsonArray = new JSONArray(response);
				int sizeJson = mJsonArray.length();
				ArrayList<MusicAlbumVideoList> mListMusicAlbumVideo = new ArrayList<MusicAlbumVideoList>();
				for (int i = 0; i < sizeJson; i++) {
					JSONObject mJsonObject = mJsonArray.getJSONObject(i);
					int musicID = mJsonObject.getInt(MUSIC_ID);
					String type = mJsonObject.getString(TYPE);

					JSONObject jsonMusicInfo = mJsonObject.getJSONObject(MUSIC_INFO);
					String thumbnail = jsonMusicInfo.getString(THUMBNAIL);
					String title = jsonMusicInfo.getString(TITLE);
					MusicInfo musicInfo = new MusicInfo(thumbnail, title);
				
					JSONObject jsonArtist = mJsonObject.getJSONObject(ARTIST);
					int id = jsonArtist.getInt(ID);
					String name = jsonArtist.getString(NAME);
					MusicArtist musicArtist = new MusicArtist(id, name);
					MusicAlbumVideoList mMusicAlbumVideoList = new MusicAlbumVideoList(musicID, musicInfo, musicArtist, type);
					mListMusicAlbumVideo.add(mMusicAlbumVideoList);
				}

				return mListMusicAlbumVideo;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
}
